<?php

class modelRh extends Zend_Db_Table_Abstract
{

    protected $_schema = 'moodle';
    protected $_name = 'mdl_user';

    public function fetchAll($where=null, $order=null, $count = null, $offset = null) {
        if (!($where instanceof Zend_Db_Table_Select)) {
            $select = $this->select();

            if ($where !== null) {
                $this->_where($select, $where);
            }

            if ($order !== null) {
                $this->_order($select, $order);
            }

            if ($count !== null || $offset !== null) {
                $select->limit($count, $offset);
            }

        } else {
            $select = $where;
        }
        $select->where('deleted=0');
        return parent::fetchAll($where, $order, $count,$offset);
    }

    public function insert(array $data) {
        $db = $this->getAdapter();
        $select = $db->select()
                ->from('moodle.mdl_user')
                ->where('username = ?',$data['username'])
                ->where('deleted = 0');
        $result = $db->fetchAll($select);
        if(count($result)==0){
            return parent::insert($data);
        } else {return null;}
    }

    public function update(array $data, $where) {
        $db = $this->getAdapter();
        $select = $db->select()
                ->from('moodle.mdl_user')
                ->where('username = ?',$data['username'])
                ->where('id <> ?',$data['id']);
        if(count($db->fetchAll($select))==0){
            return parent::update($data,$where);
        } else {return null;}
    }

    public function pesquisaRh($cpf, $nome=null, $email=null)
    {
        Zend_Db_Table::setDefaultAdapter('moodle');
        $db = Zend_Db_Table::getDefaultAdapter();
        Zend_Db_Table::setDefaultAdapter('gesac');
        $select = $db->select()->from('mdl_user');
        if($cpf)    {
            $select->where('cpf = ?',$cpf)->orWhere('username = ?',$cpf); 
            }
        if($nome)   {$select->where('nome = ?','%'.$nome.'%'); }
        if($email)  {$select->where('email = ?',$email); }
        if($cpf || $nome || $email):
            return $db->fetchRow($select);
        else:
            throw new Exception("É necessário informar pelo menos um critério de pesquisa.");
        endif;
    }

    public function getRh($id)
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Recurso humano não localizado: $id");
        }
        return $row->toArray();
    }

    public function addRh($cpf, $firstname, $lastname, $nome_guerra, $sexo, $email, $formacao, $formacao_curso, $formacao_confirmada, $servidor_dpf, $cargo, $classe, $matricula, $siape, $lotacao, $exercicio, $banco, $banco_agencia, $banco_conta, $address, $cep, $bairro, $city, $estado, $country, $telefone1, $telefone2, $telefone3, $lang, $outras_informacoes, $username, $password, $forcar_alteracao)
    {
        $data = array(
            'cpf' => $cpf,
            'firstname' => $firstname,
            'lastname' => $lastname,
            'nome_guerra' => $nome_guerra,
            'sexo' => $sexo,
            'email' => $email,
            'formacao' => $formacao,
            'formacao_curso' => $formacao_curso,
            'formacao_confirmada' => $formacao_confirmada,
            'servidor_dpf' => $servidor_dpf,
            'cargo' => $cargo,
            'classe' => $classe,
            'matricula' => $matricula,
            'siape' => $siape,
            'lotacao' => $lotacao,
            'exercicio' => $exercicio,
            'banco' => $banco,
            'banco_agencia' => $banco_agencia,
            'banco_conta' => $banco_conta,
            'address' => $address,
            'cep' => $cep,
            'bairro' => $bairro,
            'city' => $city,
            'estado' => $estado,
            'country' => $country,
            'telefone1' => $telefone1,
            'telefone2' => $telefone2,
            'telefone3' => $telefone3,
            'lang' => $lang,
            'outras_informacoes' => $outras_informacoes,
            'username' => $username,
            'password' => $password,
            'forcar_alteracao' => $forcar_alteracao
        );
        $this->insert($data);
    }

    public function updateRh($id, $cpf, $firstname, $lastname, $nome_guerra, $sexo, $email, $formacao, $formacao_curso, $formacao_confirmada, $servidor_dpf, $cargo, $classe, $matricula, $siape, $lotacao, $exercicio, $banco, $banco_agencia, $banco_conta, $address, $cep, $bairro, $city, $estado, $country, $telefone1, $telefone2, $telefone3, $lang, $outras_informacoes, $username, $password, $forcar_alteracao)
    {
        $data = array(
            'id' => $id,
            'cpf' => $cpf,
            'firstname' => $firstname,
            'lastname' => $lastname,
            'nome_guerra' => $nome_guerra,
            'sexo' => $sexo,
            'email' => $email,
            'formacao' => $formacao,
            'formacao_curso' => $formacao_curso,
            'formacao_confirmada' => $formacao_confirmada,
            'servidor_dpf' => $servidor_dpf,
            'cargo' => $cargo,
            'classe' => $classe,
            'matricula' => $matricula,
            'siape' => $siape,
            'lotacao' => $lotacao,
            'exercicio' => $exercicio,
            'banco' => $banco,
            'banco_agencia' => $banco_agencia,
            'banco_conta' => $banco_conta,
            'address' => $address,
            'cep' => $cep,
            'bairro' => $bairro,
            'city' => $city,
            'estado' => $estado,
            'country' => $country,
            'telefone1' => $telefone1,
            'telefone2' => $telefone2,
            'telefone3' => $telefone3,
            'lang' => $lang,
            'outras_informacoes' => $outras_informacoes,
            'username' => $username,
            'password' => $password,
            'forcar_alteracao' => $forcar_alteracao
        );
        $this->update($data, 'id = '. (int)$id);
    }

    public function deleteRh($id)
    {
        $this->delete('id =' . (int)$id);
    }
}